using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

using NBear.Common;

namespace NBear.IoC.Service
{
    [Serializable]
    public class ParameterCollection
    {
        private Hashtable parmValues = new Hashtable();

        public string SerializedData
        {
            get
            {
                NBear.Common.JSON.JSONObject json = new NBear.Common.JSON.JSONObject();
                foreach (string key in parmValues.Keys)
                {
                    json.put(key, parmValues[key]);
                }

                return json.ToString();
            }
            set
            {
                NBear.Common.JSON.JSONObject json = new NBear.Common.JSON.JSONObject(value);
                parmValues.Clear();
                IEnumerator en = json.keys();
                while (en.MoveNext())
                {
                    string key = (string)en.Current;
                    parmValues.Add(key, json.getString(key));
                }
            }
        }

        public ParameterCollection()
        {
        }

        public string this[string paramName]
        {
            get
            {
                if (!parmValues.ContainsKey(paramName))
                {
                    return null;
                }
                else
                {
                    return (string)parmValues[paramName];
                }
            }
            set
            {
                if (value == null)
                {
                    return;
                }

                object val = value;
                parmValues.Add(paramName, val);
            }
        }

        public void Remove(string paramName)
        {
            if (parmValues.ContainsKey(paramName))
            {
                parmValues.Remove(paramName);
            }
        }

        public void Clear()
        {
            parmValues.Clear();
        }

        public override string ToString()
        {
            return SerializedData;
        }
    }
}
